package com.malinkang.sqlite;

import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.provider.BaseColumns;
import android.support.annotation.Nullable;

/**
 * Created by malinkang on 16/3/28.
 */
public class UserContentProvider extends ContentProvider {

    public static final String TAG = UserContentProvider.class.getSimpleName();

    private DBOpenHelper mDBOpenHelper;

    private SQLiteDatabase mSQLiteDatabase;

    public static final String sAuthority = "com.malinkang.sqlite.UserContentProvider";

    /**
     * http://developer.android.com/guide/topics/providers/content-provider-creating.html#TableMIMETypes
     */
    public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + "/com.malinkang.sqlite.UserContentProvider.user";


    public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + "com.malinkang.sqlite.UserContentProvider.user";

    public static final UriMatcher sUriMatcher;

    public static final int USER_COLLECTION = 1;

    public static final int USER_SINGLE = 2;

    static {
        sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        sUriMatcher.addURI(sAuthority, "user", USER_COLLECTION);
        sUriMatcher.addURI(sAuthority, "user/#", USER_SINGLE);
    }

    @Override
    public boolean onCreate() {
        mDBOpenHelper = new DBOpenHelper(getContext());
        mSQLiteDatabase = mDBOpenHelper.getWritableDatabase();
        return false;
    }

    @Nullable
    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        Cursor cursor = null;
        switch (sUriMatcher.match(uri)) {
            case USER_COLLECTION:
                cursor = mSQLiteDatabase.query(DBOpenHelper.USER_TABLE_NAME, projection, selection, selectionArgs, null, null, null);
                if (cursor != null) {
                    cursor.setNotificationUri(getContext().getContentResolver(), uri);
                }
                return cursor;

            case USER_SINGLE:
                if (selection != null) {
                    selection = selection + BaseColumns._ID + " = " + uri.getLastPathSegment();
                } else {
                    selection = BaseColumns._ID + " = " + uri.getLastPathSegment();
                }
                cursor = mSQLiteDatabase.query(DBOpenHelper.USER_TABLE_NAME, projection, selection, selectionArgs, null, null, null);
                if (cursor != null) {
                    cursor.setNotificationUri(getContext().getContentResolver(), uri);
                }
                return cursor;
            default:
                return null;

        }

    }

    @Nullable
    @Override
    public String getType(Uri uri) {
        switch (sUriMatcher.match(uri)) {
            case USER_COLLECTION:
                return CONTENT_TYPE;
            case USER_SINGLE:
                return CONTENT_ITEM_TYPE;
            default:
                throw new IllegalArgumentException("Unknown URI" + uri);
        }
    }

    @Nullable
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        long id = mSQLiteDatabase.insert(DBOpenHelper.USER_TABLE_NAME, null, values);
        if (id != -1) {
            Uri retUri = buildUri(id);
            getContext().getContentResolver().notifyChange(uri, null);
            return retUri;
        }
        return null;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int id = mSQLiteDatabase.delete(DBOpenHelper.USER_TABLE_NAME, selection, selectionArgs);
        getContext().getContentResolver().notifyChange(uri, null);
        return id;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        int count = mSQLiteDatabase.update(DBOpenHelper.USER_TABLE_NAME, values, selection, selectionArgs);
        getContext().getContentResolver().notifyChange(uri, null);
        return count;
    }

    /**
     *
     */
    public static Uri buildUri(long id) {
        final StringBuilder uri = new StringBuilder();
        uri.append("content://");
        uri.append(sAuthority);
        uri.append("/");
        uri.append(DBOpenHelper.USER_TABLE_NAME);
        uri.append("/");
        uri.append(id);
        return Uri.parse(uri.toString());
    }
}
